﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ridofy.RiaClient.Web.Models;
using Ridofy.LastFm;
using Ridofy.Spotify;
using System.ServiceModel;

namespace Ridofy.RiaClient.Web.Services
{
    public class UserSongCollection
    {
        private string _userName;
        private ILastFmRssClient lastfm;
        private ISpotifyClient spoty;


        public UserSongCollection(string userName) : 
            this(userName, new LastFmRssClient(), new SpotifyClient())
        {                        
        }

        public UserSongCollection(string userName, 
             ILastFmRssClient lfmRss, 
            ISpotifyClient spotifyAgent)
        {
            this._userName = userName;
            this.lastfm = lfmRss;
            this.spoty = spotifyAgent;
        }

        public  IEnumerable<Songs> GetRecentSongs()
        {
            List<Songs> result = new List<Songs>();
            Track[] recentTracks = lastfm.RecentTracks(_userName);

            foreach (var item in recentTracks)
            {
                SpotifySearchResult sres =  spoty.SearchTrack(item.Title);
                if ((sres.Tracks != null) && (sres.Tracks.Length > 0))
                {
                    result.Add(new Songs() 
                    { 
                        SongName = sres.Tracks[0].Title,  
                        Album=sres.Tracks[0].SpotifyUrl, 
                        SongId = sres.Tracks[0].GetHashCode() 
                    });
                }                
            }
            return result;
        }
    }
}
